import React from 'react';
import Utils from '../../utils/utils'
import {Table,Pagination} from 'antd';


export default class LTable extends React.Component {

    pageChange=(page)=>{
        console.log(page);
    };

    onRowClick = (record, index) => {
        if(this.props.setId){
            this.props.setId(record);
        }
        let rowSelection = this.props.rowSelection
        if (rowSelection === 'checkbox') {
            let selectedRowKeys = this.props.selectedRowKeys;
            let selectedItem = this.props.selectedItem;
            let selectedIds=this.props.selectedIds;
            if(selectedIds){
                const i=selectedIds.indexOf(record.id);
                if(i===-1){
                    selectedIds.push(record.id);
                    selectedRowKeys.push(index);
                    selectedItem.push(record);
                }else{
                    selectedIds.splice(i,1);
                    selectedRowKeys.splice(i,1);
                    selectedItem.splice(i,1);
                }
            }else{
                selectedIds=[record.id];
                selectedRowKeys=[index];
                selectedItem=[record];
            }
            this.props.updateSelectedItem(selectedRowKeys,selectedItem,selectedIds);
        }else{
            let selectedRowKeys=[record.id];
            let selectedItem=record;
            this.props.updateSelectedItem(selectedRowKeys,selectedItem);
        }
    };
    tableInit = () => {
        let row_selection = this.props.rowSelection;//判断是单选、多选。默认单选，不要的话 就传false
        let selectedRowKeys = this.props.selectedRowKeys;
        const rowSelection = {
            type: 'radio',//单选
            selectedRowKeys,//当前选中某一行
            onChange: (selectedRowKeys, selectedRows) => {//单选按钮发生改变
                this.props._this.setState({
                    selectedRowKeys:selectedRowKeys,
                });
                if(this.props.setId){
                    this.props.setId(selectedRows[0]);
                }

            },
        };
        if (row_selection === false || row_selection === null) {//不需要单选或者复选
            row_selection = false
        } else if (row_selection === 'checkbox') {//多选
            rowSelection.type = 'checkbox';
        } else {
            row_selection = 'radio';
        }
        return<div>
            <Table
                {...this.props}
                bordered
                rowKey="id"
                loading={this.props.loading}
                onRow={(record, index) => {//点击table某一行
                    return {
                        onClick: () => {
                            // if (!row_selection) {//如果没有选择按钮就返回
                            //     return;
                            // }
                            this.onRowClick(record, index);
                        }
                    };
                }}
                pagination={this.props.pagination}
                rowSelection={row_selection ? rowSelection : null}
            />
        </div>
    };

    render() {
        return (<div>
            {this.tableInit()}
        </div>)
    }
}